package edu.pku.sei.transformation.structure;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import edu.pku.sei.mdabase.infrastructure.runtime.Context;
import edu.pku.sei.mdabase.infrastructure.runtime.Variable;

public class Trace {
	
	private Map<Object, List<Context>> traceMap = new HashMap<Object, List<Context>>();
	
//	public Context getTrace(Object rule)
//	{
//		return traceMap.get(rule);
//	}
//	
	public Context getContext(Context c)
	{
		Rule rule = (Rule)c.getKey();
		if (rule == null)
			return null;
		
		List<Context> results = traceMap.get(rule);
		for(Context result : results){
			Set<String> s = c.getVariables().keySet();
			for (String str : s)
			{
				Variable var = c.getVariable(str);
				Variable var2 = result.getVariable(str);
				if (!var.equal(var2))
					return null;
			}
			return result;
		}
		return null;
		
	}
	
	public boolean add(Context c, Object key)
	{
		List<Context> cList = traceMap.get(key);
		if(cList==null){
			cList = new ArrayList<Context>();
			traceMap.put(key, cList);			
		}
		
		
		cList.add(c);
		return true;
	}
	
	public List<Context> get(Rule r)
	{
		return traceMap.get(r);
	}

}
